package pl.us.ij.plugins.segmentation;

import ij.ImageListener;
import ij.ImagePlus;
import ij.WindowManager;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Iterator;
import java.util.Vector;

public class SwitchImageNotifier implements ImageListener, WindowListener {

    private Vector listeners = new Vector();

    private ImagePlus previousImage;

    SwitchImageNotifier() {

        addOpenedImages();

        ImagePlus.addImageListener(this);
    }

    public void addSwitchImageListener(SwitchImageListener l) {
        if (listeners != null) {
            if (l != null && !listeners.contains(l)) {
                listeners.add(l);
            }
        }
    }

    public void removeSwitchImageListener(SwitchImageListener l) {
        if (listeners != null) {
            if (l != null) {
                listeners.remove(l);
            }
        }
    }


    @Override
    public void imageOpened(ImagePlus imp) {
        imp.getWindow().addWindowListener(this);

        notifyListeners(true, imp);
    }

    @Override
    public void imageClosed(ImagePlus imp) {
        imp.getWindow().removeWindowListener(this);

        notifyListeners(false, imp);
    }

    @Override
    public void imageUpdated(ImagePlus imp) {
    }

    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
        notifyListeners(true, WindowManager.getCurrentImage());
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        notifyListeners(false, previousImage);
    }

    void notifyListeners() {
        if (WindowManager.getCurrentImage() != null) {
            notifyListeners(true, WindowManager.getCurrentImage());
        }
    }

    private void addOpenedImages() {
        int[] idList = WindowManager.getIDList();

        if (idList == null) {
            return;
        }

        for (int i = 0; i < idList.length; i++) {
            WindowManager.getImage(idList[i]).getWindow().addWindowListener(this);
        }
    }

    private void notifyListeners(boolean activated, ImagePlus currentImage) {

//        Trace.log("Notify called: " + listeners.size());
        Iterator i = listeners.iterator();

        while (i.hasNext()) {
            try {
                SwitchImageListener listener = (SwitchImageListener) i.next();

                if (activated) {
                    previousImage = currentImage;
                    if ( currentImage != null ) {
                        listener.imageActivated(currentImage);
//                        Trace.log("imageActivated: " + (currentImage == null ? "null" : currentImage.getShortTitle()));
                    }
                } else {
                    listener.imageDeactivated(currentImage, WindowManager.getCurrentImage());
//                    Trace.log("imageDeactivated: " + (currentImage == null ? "null" : currentImage.getShortTitle()));
                }
            }
            catch (Exception e) {
                ij.IJ.error(e.getMessage());
                ij.IJ.log(e.getMessage());
            }
        }
    }
}
